﻿//
//                       _oo0oo_
//                      o8888888o
//                      88" . "88
//                      (| -_- |)
//                      0\  =  /0
//                    ___/`---'\___
//                  .' \\|     |// '.
//                 / \\|||  :  |||// \
//                / _||||| -:- |||||- \
//               |   | \\\  -  /// |   |
//               | \_|  ''\---/''  |_/ |
//               \  .-\__  '-'  ___/-. /
//             ___'. .'  /--.--\  `. .'___
//          ."" '<  `.___\_<|>_/___.' >' "".
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//         \  \ `_.   \_ __\ /__ _/   .-` /  /
//     =====`-.____`.___ \_____/___.-`___.-'=====
//                       `=---='
//
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//               佛祖保佑         永无BUG
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#region << 版 本 注 释 >>

/*----------------------------------------------------------------
* 类 名 称 ：PlayerControlComponent
* 类 描 述 ：
* 作    者 ：henrychang
* 创建时间 ：2019/1/28 17:18:18
*******************************************************************
* Copyright @ henrychang 2018. All rights reserved.
*******************************************************************
//----------------------------------------------------------------*/

#endregion << 版 本 注 释 >>

using TouchFish.Component;
using UnityEngine;

public class PlayerControlComponent : TouchFish.Component.Component
{
    public float jiasudu = 1.8f;
    private Rigidbody rigid;
    private float distence;
    private float velocity;

    private Vector3 v3Chu, v3Zhong;
    private Vector3 offect;

    public PlayerControlComponent() : base(ComponentView.PlayerControlComponent)
    {
        distence = 0;
    }

    public override void InitAdd()
    {
        base.InitAdd();

        rigid = Actor.SelfGameObject.GetComponent<Rigidbody>();
    }

    public override void SubscribeEvents()
    {
        base.SubscribeEvents();
        Actor.OnUpdate += FixedExecute;
    }

    public override void UnsubscribeEvents()
    {
        base.UnsubscribeEvents();
        Actor.OnUpdate -= FixedExecute;
    }

    public override void FixedExecute()
    {
        base.FixedExecute();
        if (Input.GetMouseButtonDown(0))
        {
            v3Chu = Input.mousePosition;
        }
        else if (Input.GetMouseButton(0))
        {
            v3Zhong = Input.mousePosition;
            offect = v3Zhong - v3Chu;
            v3Chu = v3Zhong;
            offect = new Vector3(offect.x, 0f, offect.y);
            rigid.AddForce(offect * jiasudu, ForceMode.Acceleration);
        }
        else
        {
            rigid.MovePosition(Actor.SelfTransform.position + Vector3.forward * 0.02f);
        }
        float z = Camera.main.transform.position.z;
        z = Mathf.Clamp(Actor.SelfTransform.position.z, Camera.main.transform.position.z + 2.7f, Mathf.Infinity);
        float x = Mathf.Clamp(Actor.SelfTransform.position.x, -4.2f, 4.2f);
        Actor.SelfTransform.position = new Vector3(x, Actor.SelfTransform.position.y, z);
    }
}